package com.example.assetmanagement.controller;

import com.example.assetmanagement.common.ApiResponse;
import com.example.assetmanagement.common.PageRequest;
import com.example.assetmanagement.common.PageResponse;
import com.example.assetmanagement.domain.Role;
import com.example.assetmanagement.service.RoleService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    @GetMapping("/list")
    public ApiResponse<PageResponse<Role>> list(@Valid PageRequest pageRequest,
                                                @RequestParam(required = false) String name,
                                                @RequestParam(required = false) Integer status) {
        return ApiResponse.success(roleService.list(pageRequest, name, status));
    }

    @GetMapping("/all")
    public ApiResponse<List<Role>> all() {
        return ApiResponse.success(roleService.getAll());
    }

    @GetMapping("/{id}")
    public ApiResponse<Role> detail(@PathVariable Long id) {
        return ApiResponse.success(roleService.getById(id));
    }

    @PostMapping
    public ApiResponse<?> add(@RequestBody @Valid Role role) {
        roleService.add(role);
        return ApiResponse.success(null);
    }

    @PutMapping("/{id}")
    public ApiResponse<?> update(@PathVariable Long id, @RequestBody @Valid Role role) {
        roleService.update(id, role);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/{id}")
    public ApiResponse<?> delete(@PathVariable Long id) {
        roleService.delete(id);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/batch")
    public ApiResponse<?> batchDelete(@RequestBody List<Long> ids) {
        roleService.batchDelete(ids);
        return ApiResponse.success(null);
    }

    @PutMapping("/{id}/status")
    public ApiResponse<?> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        roleService.updateStatus(id, status);
        return ApiResponse.success(null);
    }
} 